/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLThuVien.BLL;

import QLThuVien.DAL.MyConnectUnit;
import QLThuVien.DTO.*;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Rado
 */
public class BBCTraTre {
    MyConnectUnit connect;
    
    public BBCTraTre(){
        //this.connect = new MyConnectUnit("localhost", "root", "123456", "QLTV");
        this.connect = new MyConnectUnit("", "sa", "123456", "QLTV");
    }
    
    public HashSet<BCTraTre> getBCTraTres(String Condition, String OrderBy) throws Exception{
         ResultSet result = this.connect.Select("BC_TRATRE", Condition, OrderBy);
         HashSet<BCTraTre> bctratres = new HashSet<BCTraTre>();
         while(result.next()){
             BCTraTre bctratre = new BCTraTre();
             bctratre.setNgay(result.getString("Ngay"));
             bctratre.setMaCuonSach(result.getString("MaCuonSach"));
             bctratre.setNgayMuon(result.getString("NgayMuon"));
             bctratre.setSoNgayTraTre(result.getString("SoNgayTraTre"));             
             bctratres.add(bctratre);
         }
         return bctratres;
     }
    
    public HashSet<BCTraTre> getBCTraTres(String Condition) throws Exception{
        return getBCTraTres(Condition, null);
    }
    
    public HashSet<BCTraTre> getBCTraTres() throws Exception{
        return getBCTraTres(null);
    }
    
    public BCTraTre getByID(String ngay, String macuonsach) throws Exception{
        HashSet<BCTraTre> bctratres = this.getBCTraTres("Ngay = " + ngay + "AND MaCuonSach = " + macuonsach);
        if(bctratres.size() > 0)
            return bctratres.toArray(new BCTraTre[bctratres.size()])[0];
        return null;
    }
    
    public void InsertBCTraTre(BCTraTre bctratre) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("Ngay", bctratre.getNgay());       
        map.put("MaCuonSach", bctratre.getMaCuonSach());
        map.put("NgayMuon", bctratre.getNgayMuon());       
        map.put("SoNgayTraTre", bctratre.getSoNgayTraTre()); 
        this.connect.Insert("BC_TRATRE", map);
    }
    
    public void InsertBCTraTre(HashSet<BCTraTre> bctratres) throws Exception{
        for(BCTraTre bctratre:bctratres)
            this.InsertBCTraTre(bctratre);
    }
    
    public void UpdateBCTraTre(BCTraTre bctratre) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>(); 
        map.put("NgayMuon", bctratre.getNgayMuon());       
        map.put("SoNgayTraTre", bctratre.getSoNgayTraTre());
        this.connect.Update("BC_TRATRE", map, "Ngay = " + bctratre.getNgay() + "AND MaCuonSach = " + bctratre.getMaCuonSach());
    }
    
    public void UpdateBCTraTre(HashSet<BCTraTre> bctratres) throws Exception{
        for(BCTraTre bctratre:bctratres)
            this.UpdateBCTraTre(bctratre);
    }
    
    public void DeleteBCTraTre(BCTraTre bctratre) throws Exception{
        this.connect.Delete("BC_TRATRE", "Ngay = " + bctratre.getNgay() + "AND MaCuonSach = " + bctratre.getMaCuonSach() +"");
    }
    
    public void DeleteBCTraTre(HashSet<BCTraTre> bctratres) throws Exception{
        for(BCTraTre bctratre:bctratres)
            this.DeleteBCTraTre(bctratre);
    }
}
